package com.example.demo.util;

import com.example.demo.PO.Lesson;

import java.util.*;

public class Counter {

    //因為全部課程都是秋季學期的課,有些課的學分無從獲取,只能統一設為2分
    public Map<String,Double> creditCompositionCounter(List<Lesson> allLesson, List<String> requiredGeneralCourse, List<String> requiredCoreCourse, List<String> takenCourse){
        Map<String, Double> creditComposition = new HashMap<>();
        creditComposition.put("全部", 0.0);
        creditComposition.put("通识", 0.0);
        creditComposition.put("通修", 0.0);
        creditComposition.put("平台核心", 0.0);
        creditComposition.put("选修", 0.0);
        creditComposition.put("輔修", 0.0);
        creditComposition.put("科学之光", 0.0);
        creditComposition.put("阅读",0.0);
        creditComposition.put("美育", 0.0);
        creditComposition.put("劳育", 0.0);
        for(int i = 0 ; i < takenCourse.size(); i++){
            String courseKey = takenCourse.get(i);
            String courseNo = courseKey.substring(0, 8);
            if(Character.isLetter(courseKey.toCharArray()[8])){
                courseNo += courseKey.toCharArray()[8];
            }
            takenCourse.set(i,courseNo);
        }
        List<String> copyCourse = new ArrayList<>(takenCourse);
        int reading = 0;
        for(String courseNo: takenCourse){


            if(courseNo.startsWith("002")|| courseNo.startsWith("003")|| courseNo.startsWith("004")|| courseNo.startsWith("005")|| courseNo.startsWith("37")|| courseNo.startsWith("500")){
                for(Lesson lesson : allLesson){
                    if(lesson.get课程号().equals(courseNo)){
                        if(lesson.get课程名().startsWith("“科学之光”")){
                            creditComposition.put("科学之光", 1.0);
                        }
                        if(courseNo.equals("00372480A")){
                            creditComposition.put("劳育", 1.0);
                        }
                        if(courseNo.equals("00302390S")){
                            creditComposition.put("美育", 1.0);
                        }
                        if(courseNo.startsWith("004")){
                            reading++;
                            if(reading == 3){
                                creditComposition.put("阅读",creditComposition.get("阅读")+1.0);
                                creditComposition.put("通识", creditComposition.get("通识") +1.0);
                                creditComposition.put("全部", creditComposition.get("全部") + 1.0);
                            }
                            if(reading == 6){
                                creditComposition.put("阅读",creditComposition.get("阅读")+1.0);
                                creditComposition.put("通识", creditComposition.get("通识") +1.0);
                                creditComposition.put("全部", creditComposition.get("全部") + 1.0);
                            }

                        }
                        creditComposition.put("通识", creditComposition.get("通识") + Double.parseDouble(lesson.get学分()));
                        creditComposition.put("全部", creditComposition.get("全部") + Double.parseDouble(lesson.get学分()));
                        copyCourse.remove(courseNo);
                        break;
                    }
                }
            }else if(requiredGeneralCourse.contains(courseNo)){
                for(Lesson lesson : allLesson){
                    if(lesson.get课程号().equals(courseNo)){
                        creditComposition.put("通修",creditComposition.get("通修")+Double.parseDouble(lesson.get学分()));
                        creditComposition.put("全部", creditComposition.get("全部") + Double.parseDouble(lesson.get学分()));
                        copyCourse.remove(courseNo);
                        break;
                    }
                }
            }else if(requiredCoreCourse.contains(courseNo)){
                boolean find = false;
                for(Lesson lesson : allLesson){
                    if(lesson.get课程号().equals(courseNo)){
                        find = true;
                        creditComposition.put("平台核心",creditComposition.get("平台核心")+Double.parseDouble(lesson.get学分()));
                        creditComposition.put("全部", creditComposition.get("全部") + Double.parseDouble(lesson.get学分()));
                        copyCourse.remove(courseNo);
                        break;
                    }


                }
                if(!find){
                    creditComposition.put("平台核心",creditComposition.get("平台核心") + 2.0);
                    creditComposition.put("全部", creditComposition.get("全部") + 2.0);
                }
            }

        }
        for(String courseNo: copyCourse){

            boolean find = false;
            for(Lesson lesson : allLesson) {
                if (lesson.get课程号().equals(courseNo)) {
                    find = true;
                    creditComposition.put("选修",creditComposition.get("选修")+Double.parseDouble(lesson.get学分()));
                    creditComposition.put("全部", creditComposition.get("全部") + Double.parseDouble(lesson.get学分()));
                    break;
                }
            }
            if(!find){
                creditComposition.put("选修",creditComposition.get("选修") + 2.0);
                creditComposition.put("全部", creditComposition.get("全部") + 2.0);
            }

        }
        return creditComposition;
    }
}
